package com.qianfeng.xinyihezi.xinyihezi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qianfeng.xinyihezi.xinyihezi.R;
import com.qianfeng.xinyihezi.xinyihezi.model.GoodsData;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Tcoy on 2016/11/15.
 */

public class GoodsGridViewAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater;
    private List<GoodsData> goodsDataList;
    private IBackcallData iBackcallData;

    public GoodsGridViewAdapter(Context context, List<GoodsData> goodsDataList) {
        this.context = context;
        this.goodsDataList = goodsDataList;
        inflater = LayoutInflater.from(context);
    }

    @Override

    public int getCount() {
        return goodsDataList.size();
    }

    @Override
    public Object getItem(int position) {
        return goodsDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.gridview_item_goods_layout, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.imgPic.setTag(goodsDataList.get(position));
        if (iBackcallData != null) {
            viewHolder.imgPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    iBackcallData.backGoods(v);
                }
            });
        }
        
        Picasso.with(context).load(goodsDataList.get(position).getImage_url()).into(viewHolder.imgPic);
        viewHolder.tvTitle.setText(goodsDataList.get(position).getShort_name() + "                                              ");
        viewHolder.tvPrice.setText("¥" + goodsDataList.get(position).getP_price());
        viewHolder.tvLike.setText("" + goodsDataList.get(position).getLike_count());
        return convertView;
    }

    public class ViewHolder {
        private ImageView imgPic;
        private TextView tvTitle, tvPrice, tvLike;

        public ViewHolder(View view) {
            imgPic = (ImageView) view.findViewById(R.id.imageViewGridPic);
            tvTitle = (TextView) view.findViewById(R.id.textViewTitle);
            tvPrice = (TextView) view.findViewById(R.id.textViewGridPrice);
            tvLike = (TextView) view.findViewById(R.id.textViewLike);
        }
    }

    public interface IBackcallData {
        void backGoods(View view);
    }

    public void setIBackCall(IBackcallData iBackcallData) {
        this.iBackcallData = iBackcallData;
    }
}
